import { cloneElement, PropsWithChildren, ReactElement } from "react";
import { useModel } from "@umijs/max";
import { openLoginModal } from "@/components/LoginModal";
import { message } from "antd";

export default function MustLogin(props: PropsWithChildren){
  const {data} = useModel('user', (({data})=>({data})));

  return cloneElement(props.children as ReactElement, {
    onClick: (e)=>{
      if(!data){
        message.warning({
          content: '请先登录',
          key: '请先登录'
        });
        e.preventDefault();
        openLoginModal();
        return;
      }
      // @ts-ignore
      props.children?.props?.onClick?.(e);
    }
  })
}
